import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录', requiresAuth: false, icon: 'User' }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('../layouts/default.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'personnel',
        name: 'Personnel',
        component: () => import('../views/system/index.vue'),
        meta: { title: '人员管理', icon: 'UserFilled' },
        children: [
          {
            path: '/system/departments',
            name: 'Departments',
            component: () => import('../views/system/departments/index.vue'),
            meta: { title: '部门管理', icon: 'OfficeBuilding' }
          },
          {
            path: '/system/positions',
            name: 'Positions',
            component: () => import('../views/system/positions/index.vue'),
            meta: { title: '岗位管理', icon: 'Postcard' }
          },
          {
            path: '/system/employees',
            name: 'Employees',
            component: () => import('../views/system/employees/index.vue'),
            meta: { title: '员工管理', icon: 'UserFilled' }
          }
        ]
      },
      {
        path: 'performance',
        name: 'Performance',
        component: () => import('../views/system/index.vue'),
        meta: { title: '绩效管理', icon: 'TrendCharts' },
        children: [
          {
            path: '/performance/kpis',
            name: 'KPIs',
            component: () => import('../views/system/kpis/index.vue'),
            meta: { title: '指标管理', icon: 'Aim' }
          },
          {
            path: '/performance/assessments',
            name: 'Assessments',
            component: () => import('../views/system/assessments/index.vue'),
            meta: { title: '考核管理', icon: 'List' }
          },
          {
            path: '/performance/assessment-fills',
            name: 'AssessmentFills',
            component: () => import('../views/system/assessment-fills/index.vue'),
            meta: { title: '考核填写', icon: 'Edit' }
          },
          {
            path: '/performance/reports',
            name: 'Reports',
            component: () => import('../views/system/reports/index.vue'),
            meta: { title: '绩效报表', icon: 'TrendCharts' }
          },
          {
            path: '/performance/salary',
            name: 'Salary',
            component: () => import('../views/system/salary/index.vue'),
            meta: { title: '工资管理', icon: 'Setting' }
          },
          {
            path: '/performance/attendance',
            name: 'Attendance',
            component: () => import('../views/system/attendance/index.vue'),
            meta: { title: '考勤管理', icon: 'Calendar' }
          }
        ]
      },
      {
        path: 'system',
        name: 'System',
        component: () => import('../views/system/index.vue'),
        meta: { title: '系统管理', icon: 'Setting' },
        children: [
          {
            path: '/system/users',
            name: 'Users',
            component: () => import('../views/system/users/index.vue'),
            meta: { title: '用户管理', icon: 'User' }
          },
          // {
          //   path: '/system/roles',
          //   name: 'Roles',
          //   component: () => import('../views/system/roles/index.vue'),
          //   meta: { title: '角色管理', icon: 'Lock' }
          // },
          {
            path: '/system/announcements',
            name: 'Announcements',
            component: () => import('../views/system/announcements/index.vue'),
            meta: { title: '公告管理', icon: 'Document' }
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next({ name: 'Login' })
  } else {
    next()
  }
})

export default router